package OrederManagement.command;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import OrederManagement.command.impl.AddgoodsCommand;
import OrederManagement.command.impl.DeleteGoodsCommand;
import OrederManagement.command.impl.ExitCommand;
import OrederManagement.command.impl.HelpStoreCommand;
import OrederManagement.command.impl.ModifyGoodsCommand;
import OrederManagement.command.impl.SelectGoodsCommand;
import OrederManagement.pojo.Store;
import OrederManagement.pojo.User;

public class StoreCommandFactory extends UserCommandFactory {

	private static Map<CommandCode,Class<? extends Command>> storeCommandMap =new HashMap<>();
	static{
		storeCommandMap.put(CommandCode.HELP, HelpStoreCommand.class);
		storeCommandMap.put(CommandCode.SELECT, SelectGoodsCommand.class);
		storeCommandMap.put(CommandCode.ADD, AddgoodsCommand.class);
		storeCommandMap.put(CommandCode.MODIFY, ModifyGoodsCommand.class);
		storeCommandMap.put(CommandCode.DELETE, DeleteGoodsCommand.class);
		storeCommandMap.put(CommandCode.EXIT, ExitCommand.class);
		
	}
	private Store store=null;
	public StoreCommandFactory(User user,Store store) {
		super(user);
		this.store=store;
	}
	public Store getStore(){
		return store;
	}

	@Override
	public Command buildCommand(CommandCode cmdCode) {
		Class<?extends Command> cmdClass =storeCommandMap.get(cmdCode);
		if(cmdClass==null){
			return super.buildCommand(cmdCode);
		}
		Command command =null;
		try {
			command=cmdClass.getConstructor(User.class,Store.class).newInstance(getUser(),getStore());
		} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
				| NoSuchMethodException | SecurityException e) {
			e.printStackTrace();
		}
		
		return command;
	}

}
